import { getlogin, logout, getinfo, getroles } from '@/api/login'
import { setToken, removeToken } from '@/api/cookie'
import { setpass, setQuestion, updatePro, findPass, getQuestion } from '@/api/user'
export default {
    namespaced: true,
    state: {
        role: {},
        id: 0,
        name: '',
        avatar: '',
        introduction: '',
        roles: '',
        items: [],
        id: 0
    },
    mutations: {
        getinfo(state, res) {
            state.role = res.data
        },
        SET_ID(state, id) {
            state.id = id
        },
        SET_NAME(state, name) {
            state.name = name
        },
        SET_AVATAR(state, avatar) {
            state.avatar = avatar
        },
        SET_INTRODUCTION(state, introduction) {
            state.introduction = introduction
        },
        SET_ROLES(state, res) {
            state.roles = res.data.roles[0]
        },
<<<<<<< HEAD
        SET_ITEMS(state, res) {
            state.items = JSON.parse(res.data.items)
=======
        SET_ITEMS(state, res){
            state.items = JSON.parse(res.data.items);
            state.id = res.data.id
>>>>>>> bfd3cbb4a4da24bece4536b553d0950467ff99c4
        }
    },

    actions: {
        async  login({ commit }, form) {
            const res = await getlogin(form);
            if (!res.data.data) {
                return res
            }
            setToken(res.data.data.token)

            return res
        },
        async Logout({ commit }, id) {
            const res = await logout(id);
            removeToken();
            location.replace('/')
        },
        async setinfo({ commit }, data) {
            const res = await getinfo(data);
            commit('SET_ID', res.data.data.id)
            commit('SET_NAME', res.data.data.name)
            commit('SET_AVATAR', res.data.data.avatar)
            commit('SET_INTRODUCTION', res.data.data.introduction)
            commit('getinfo', res.data)
            commit('SET_ROLES', res.data)
        },
        async getitems({ commit }, roles) {
            const { data } = await getroles({ roles });
            commit('SET_ITEMS', data)
        },
        async setPass({ commit }, data) {
            const res = await setpass(data)
            return res
        },
        async setQuestion({ commit }, data) {
            const res = await setQuestion(data)
            return res
        },
        async updatePro({ commit }, data) {
            const res = await updatePro(data)
            return res
        },
        async findPass({ commit }, data) {
            const res = await findPass(data)
            return res
        },
        async getQuestion(context, data) {
            const res = await getQuestion(data)
            return res
        }
    },
    getters: {}
}